<?php

/**
 * 定义 Controller_BoProduct 类
 *
 * @copyright Copyright (c) 2004 - 2009 iTeam
 * @author iTeam Monkey
 * @package XingYaoDianZi
 * @version 200912241027
 */
FLEA::loadClass('Controller_BoBase');

class Controller_BoProduct extends Controller_BoBase {

    var $_modelClasses;
    var $_modelArticles;
    var $_modelAttachments;
    var $access;
    var $catid;
    var $_helperFunction;
    var $dbo;

    //构造函数
    function Controller_BoProduct() {
        parent::Controller_BoBase();
        $this->access = PRODUCT_ADMIN;
        $this->catid = PRODUCT_CLASS;
        //用户角色权限控制
        if (!$this->checkDispatcher($this->access)) {
            redirect(url('BoDashboard', 'ErrorIn'));
        }
        $this->_modelClasses = & FLEA::getSingleton('Model_Categories');
        $this->_modelArticles = & FLEA::getSingleton('Model_Articles');
        $this->_modelAttachments = & FLEA::getSingleton('Model_Attachments');
        $this->_helperFunction = & FLEA::getSingleton('Helper_Function');
        $this->dbo = &FLEA::getDBO();
    }

    //显示文章列表
    function actionIndex() {
        $catid = $this->catid;
        //查询当前分类
        $field = array('class_id' => $catid);
        if ($_REQUEST['title'])
            $field[] = "title like '%" . $_REQUEST['title'] . "%'";
        if ($_REQUEST['cat'] && $_REQUEST['cat'] != $catid)
            $field['pcid'] = $_REQUEST['cat'];
        if ($_REQUEST['rstime'])
            $field[] = "dateline > " . strtotime($_REQUEST['rstime'] . ' 00:00:00');
        if ($_REQUEST['retime'])
            $field[] = "dateline < " . strtotime($_REQUEST['retime'] . ' 00:00:00');
        $sprice = $_REQUEST['sprice'];
        $eprice = $_REQUEST['eprice'];
        if ($sprice)
            $field[] = "price >= " . $sprice;
        if ($eprice)
            $field[] = "price < " . $eprice;
        $page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $table = & $this->_modelArticles->getTable();
        $pager = & new FLEA_Helper_Pager($table, $page, 10, $field, 'hot DESC,dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", $field);
        $this->_setBack();
        $pclass = $this->_modelClasses->getClass($catid);
        $subclasses = $this->_modelClasses->getSubClasses($pclass);
//        dump($this->dbo->log);
        $listTitle = "主题婚礼列表";
        include(TPL_DIR . '/BoArticlesList.php');
    }

    // 添加产品
    function actionCreate() {
        $table = & $this->_modelArticles->getTable();
        $article = $this->_prepareData($table->meta);
        $this->_editArticle($article);
    }

    //保存
    function actionSave() {
        __TRY();
        $user = $this->getUser();
        if (!$_POST['article_id'])
            $_POST['dateline'] = time();
        $_POST['updated'] = time();
        $_POST['author'] = $user['ID'];
        $_POST['content'] = $_POST['spaw1'];
        $_POST['class_id'] = $this->catid;
        $_POST['price'];
        $errorMessage = $this->_uploadPicture('postfile');
        if ($errorMessage && is_array($errorMessage)) {
            $_POST['image_src'] = $errorMessage['filename']; //上传图片
            //FLEA::loadHelper('image');
            //$image =& FLEA_Helper_Image::createFromFile(FLEA::getAppInf('uploadDir') . DS . $_POST['image_src']);
            //$imagesize = getimagesize(FLEA::getAppInf('uploadDir') . DS . $_POST['image_src']);
            //$image->crop(200, 107, true, true);
            //$image->saveAsJpeg(FLEA::getAppInf('uploadDir') . DS . 'thumb' . DS . $_POST['image_src']);
            //$image->destory();
        }else
            unset($_POST['image_src']);
        $errorMessage = $this->_uploadPicture('postfile1');
        if ($errorMessage && is_array($errorMessage)) {
            $_POST['ext5'] = $errorMessage['filename']; //上传图片
        }else
            unset($_POST['ext5']);
        $arid = $this->_modelArticles->saveArticle($_POST);
        if ($_POST['article_id'])
            $articleid = $_POST['article_id'];
        else
            $articleid = $arid;
        $this->_modelAttachments->updateAttachments($articleid);
//        dump($this->dbo->log);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
            return $this->_editArticle($_POST, $ex->getMessage());
        }
        redirect($this->_url('Index'));
    }

    //修改文章
    function actionEdit() {
        $article = $this->_modelArticles->getArticle($_GET['id']);
        $this->_editArticle($article);
    }

    //显示文章信息编辑页面
    function _editArticle($article, $errorMessage = '') {
//    	var_dump($errorMessage);
        $catid = $this->catid;
        $table = & $this->_modelArticles->getTable();
        $pk = $table->primaryKey;
        $price = $article['price'];
        $pclass = $this->_modelClasses->getClass($catid);
        $subclasses = $this->_modelClasses->getSubClasses($pclass);
        include(TPL_DIR . '/BoProductEdit.php');
    }

    //删除资讯
    function actionRemove() {
        $this->_modelArticles->removeArticle($_GET['id']);
        $this->_goBack();
    }

    //上传更多图片
    function actionUpload() {
        $uploader = & FLEA::getSingleton('FLEA_Helper_FileUploader');
        $key = 'Filedata';
        // 检查上传文件是否存在
        if ($uploader->isFileExist($key)) {
            // 检查文件扩展名是否是允许上传的类型
            $file = & $uploader->getFile($key);
            if (!$file->check(FLEA::getAppInf('photoFileExts'), FLEA::getAppInf('photoMaxFilesize'))) {
                $errorMessage = _T('ui_p_invalid_filetypesize');
            }
            // 上传图片
            __TRY();
            $filetype = $file->getExt();
            $filename = 'guoguo' . time() . '-' . $file->getFilename();
            $file->move(FLEA::getAppInf('uploadDir') . DS . $filename);
            // 生成缩略图（220 x 220 像素大小）
            FLEA::loadHelper('image');
            $image = & FLEA_Helper_Image::createFromFile(FLEA::getAppInf('uploadDir') . DS . $filename, $filetype);
            $imagesize = getimagesize(FLEA::getAppInf('uploadDir') . DS . $filename);
            $image->crop(intval(60 * number_format($imagesize[0] / $imagesize[1], 2)), 60, true, true);
            $image->saveAsJpeg(FLEA::getAppInf('uploadDir') . DS . 'thumb' . DS . $filename);
            $image->destory();
            $ex = __CATCH();
            if (__IS_EXCEPTION($ex)) {
                $errorMessage = $ex->getMessage();
            } else {
                $admin = $this->getUser();
                $data = array();
                $data['filepath'] = $filename;
                $data['dateline'] = time();
                $data['aid'] = $admin['ID'];
                $data['articleid'] = 0;
                $save = $this->_modelAttachments->saveAttachments($data);
            }
        }
        if ($save) {
            session_start();
            if (!session_is_registered('nowPic'))
                session_register('nowPic');
            $nowPic = (array) $_SESSION['nowPic'];
            $nowPic[] = $save;
            $_SESSION['nowPic'] = $nowPic;
        }
    }

    //图片列表
    function actionPicture() {
        $articleid = isset($_GET['articleid']) ? (int) $_GET['articleid'] : null;
        if (!$articleid)
            js_alert(_T('ui_p_create_class_first'), '', url('BoProfession'));
        $field = array('articleid' => $articleid);
        $table = & $this->_modelAttachments->getTable();
        $pk = $table->primaryKey;
        $page = isset($_GET['page']) ? (int) $_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $pager = & new FLEA_Helper_Pager($table, $page, 5, $field, 'dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*", $field);
        $this->_setBack();
        $listTitle = "产品图片";
        $path = "../Upload/";
        include(TPL_DIR . '/BoPicList.php');
    }

    //删除图片
    function actionRemovePic() {
        $this->_modelAttachments->removeAttachments($_GET['atid']);
        $this->_goBack();
    }

}
